<template>
  <div class="system-add-dic-container">
    <el-dialog title="新增字典" v-model="isShowDialog" width="769px">
      <el-alert title="半成品，交互过于复杂，请自行扩展！" type="warning" :closable="false" class="mb20"></el-alert>
      <el-form :model="ruleForm" size="default" label-width="90px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="字典名称">
              <el-input v-model="ruleForm.name" placeholder="请输入字典名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="字段名">
              <el-input v-model="ruleForm.code" placeholder="请输入字段名" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="字典状态">
              <el-switch v-model="ruleForm.status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-row :gutter="35" v-for="(v, k) in ruleForm.content" :key="k">
              <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                <el-form-item :prop="`content[${k}].key`">
                  <template #label>
                    <el-button type="primary" circle size="small" @click="onAddRow" v-if="k === 0">
                      <el-icon>
                        <ele-Plus/>
                      </el-icon>
                    </el-button>
                    <el-button type="danger" circle size="small" @click="onDelRow(k)" v-else>
                      <el-icon>
                        <ele-Delete/>
                      </el-icon>
                    </el-button>
                    <span class="ml10">字段</span>
                  </template>
                  <el-input v-model="v.key" style="width: 100%" placeholder="请输入字段名"></el-input>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                <el-form-item label="属性" :prop="`content[${k}].value`">
                  <el-input v-model="v.value" style="width: 100%" placeholder="请输入属性值"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="字典描述">
              <el-input v-model="ruleForm.describes" type="textarea" placeholder="请输入字典描述" maxlength="150"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">新 增</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import {reactive, toRefs, defineComponent} from 'vue';
import { toRaw } from '@vue/reactivity'
import {useDicApi} from "/@/api/dic";

export default defineComponent({
  name: 'systemAddDic',
  setup() {
    const state = reactive({
      isShowDialog: false,
      ruleForm: {
        name: '', // 字典名称
        code: '', // 字段名
        status: true, // 字典状态
        content: [
          // 子集字段 + 属性值
          {
            key: '',
            value: '',
          },
        ],
        describes: '', // 字典描述
      },
    });
    // 打开弹窗
    const openDialog = () => {
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 新增
    const onSubmit = async () => {
      await useDicApi().add(toRaw(state.ruleForm));
      closeDialog();
    };
    // 新增行
    const onAddRow = () => {
      state.ruleForm.content.push({
        key: '',
        value: '',
      });
    };
    // 删除行
    const onDelRow = (k: number) => {
      state.ruleForm.content.splice(k, 1);
    };
    return {
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      onAddRow,
      onDelRow,
      ...toRefs(state),
    };
  },
});
</script>
